package order;

import javax.swing.JOptionPane;

import model.Player;
import model.Spy;
import model.Territory;
import model.Troop;

public class ConvertBackSpy extends UpgradeOrder{
   private Spy spy;
   private Player player;
   private Territory territory;
   private int cost = 5;
   
   public ConvertBackSpy(Spy spy){
	   this.spy = spy;
	   player = spy.getOwner();
	   territory = spy.getTerritory();
   }
   
   public Territory getSource(){return territory;}
   public Territory getDestination(){return territory;}
   
   public boolean checkOrder(){
	   if (player.getTechnology() < cost){
   		JOptionPane.showMessageDialog(null, territory.getLocation() + "No enough technology to convert back a spy!");
   		return false;
   	}
   	return true;
   }
   
   public void undoOrder(){
	   player.addTechnology(cost);
  	   Troop tr = territory.peekTroop(spy.getLevel(), player);
  	   spy= new Spy(player, tr, territory);
  	   player.addSpy(spy);
  	   territory.removeTroop(spy.getLevel(), player);
   }
   
   public  String printOrder(){
   	StringBuffer sentence = new StringBuffer();
   	sentence.append("At " + territory.getLocation() + ": ");
   	sentence.append("Convert a spy back to"  + spy.getLevel());
   	sentence.append(" (Cost 5 Technology) ");
   	return sentence.toString();
   }

@Override
public void executeOrder() {
	   player.cutTechnology(cost);
	   Troop tr = spy.covertBack();
	   player.removeSpy(spy);
	   spy.getTerritory().addTroop(tr);
	
}
}

